<?php
// .-----------------------------------------------------------------------------------
// | WE TRY THE BEST WAY 杭州博也网络科技有限公司
// |-----------------------------------------------------------------------------------
// | Author: 贝贝 <hebiduhebi@163.com>
// | Copyright (c) 2013-2016, http://www.itboye.com. All Rights Reserved.
// |-----------------------------------------------------------------------------------
namespace Shop\Api;

use Common\Api\Api;

use Shop\Model\ProductGroupModel;

class ProductGroupApi extends  Api{

    /**
     * 查询，不分页
     */
    const QUERY_NO_PAGING = "Shop/ProductGroup/queryNoPaging";
    /**
     * 添加
     */
    const ADD = "Shop/ProductGroup/add";
    /**
     * 保存
     */
    const SAVE = "Shop/ProductGroup/save";
    /**
     * 保存根据ID主键
     */
    const SAVE_BY_ID = "Shop/ProductGroup/saveByID";

    /**
     * 删除
     */
    const DELETE = "Shop/ProductGroup/delete";

    /**
     * 查询
     */
    const QUERY = "Shop/ProductGroup/query";
    /**
     * 查询一条数据
     */
    const GET_INFO = "Shop/ProductGroup/getInfo";

    const QUERY_WITH_PRODUCT="Shop/ProductGroup/queryWithProduct";


    /**
     * 获取商品分组信息
     */
    const GET_GROUP_BY_ID = "Shop/ProductGroup/getGroupsById";

	protected function _init(){
		$this->model = new ProductGroupModel();
	}

    public function queryWithProduct($map = null,$page = array('curpage'=>1,'size'=>10),$order = false,$params = false,$field = false){

        $main_img_type = 6015;//主图

        $query = $this->model->alias('g')->join("left join itboye_product as p on p.id = g.p_id")->join('(select img_id as img,pid from itboye_product_image where type='.$main_img_type.') as image on g.p_id=image.pid','left')->join('(select min(price) as price,product_id from itboye_product_sku group by product_id) as sku on g.p_id=sku.product_id','inner')->join('itboye_product_attr on g.p_id=itboye_product_attr.pid','left');

        $query = $query -> page($page['curpage'] . ',' . $page['size']);

        if($map !== null){
            $query = $query -> where($map);
        }

        if($order !== false){
            $query = $query -> order($order);
        }

        if($field !== false){
            $query = $query -> field($field);
        }
        $list = $query -> select();

        if ($list === false) {
            $error = $this -> model -> getDbError();
            return $this -> apiReturnErr($error);
        }

        $query = $this->model->alias('g')->join("left join itboye_product as p on p.id = g.p_id")->join('(select img_id as img,pid from itboye_product_image where type='.$main_img_type.') as image on g.p_id=image.pid','left')->join('(select min(price) as price,product_id from itboye_product_sku group by product_id) as sku on g.p_id=sku.product_id','inner')->join('itboye_product_attr on g.p_id=itboye_product_attr.pid','left');

        if($map){
            $query = $query -> where($map);
        }
        $count = $query -> count();

        $Page = new \Think\Page($count, $page['size']);

        //分页跳转的时候保证查询条件
        if ($params !== false) {
            foreach ($params as $key => $val) {
                $Page -> parameter[$key] = urlencode($val);
            }
        }

        $show = $Page -> show();

        return $this -> apiReturnSuc(array("count" => $count,"show" => $show,"list" => $list));
    }

    public function getGroupsById($pid){

        $map = array(
            'p_id' => $pid,
        );

        $now = time();
        $map['start_time'] = array('ELT',$now);
        $map['end_time'] = array('EGT',$now);

        $result = $this -> model -> join("left join common_datatree on g_id = common_datatree.id") -> where($map) -> field("p_id,g_id,start_time,end_time,common_datatree.name as name") -> select();

        if($result === false){
            $error = $this -> model -> getDbError();
            return $this -> apiReturnErr($error);
        }

        return $this -> apiReturnSuc($result);

    }
	
}

